package com.huanxing.cloud.mall.api.event.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.huanxing.cloud.common.core.constant.CommonConstants;
import com.huanxing.cloud.mall.api.event.HxOrderPayEvent;
import com.huanxing.cloud.mall.api.service.*;
import com.huanxing.cloud.mall.common.entity.CouponUser;
import com.huanxing.cloud.mall.common.entity.OrderInfo;
import com.huanxing.cloud.mall.common.entity.OrderItem;
import com.huanxing.cloud.mall.common.entity.UserInfo;
import com.huanxing.cloud.mall.common.enums.CouponUserStatusEnum;
import com.huanxing.cloud.mall.common.enums.OrderStatusEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author: lijx
 * @date: 2023/4/24 11:57
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class HxOrderPayEventListener {

	private final ICouponUserService couponUserService;

	private final IGoodsSpuService goodsSpuService;

	private final IOrderInfoService orderInfoService;

	private final IUserInfoService userInfoService;

	private final IDistributionOrderService distributionOrderService;

	@EventListener(HxOrderPayEvent.class)
	public void hxPayEventListener(HxOrderPayEvent event) {
		// 获取订单信息
		final OrderInfo orderInfo = event.getOrder();
		if (null == orderInfo) {
			log.warn("order not found!");
			return;
		}
		orderInfo.setStatus(OrderStatusEnum.STATUS_2.getCode());
		orderInfo.setPayStatus(CommonConstants.YES);
		orderInfoService.updateById(orderInfo);
	}

	@EventListener(HxOrderPayEvent.class)
	public void hxPayCouponEventListener(HxOrderPayEvent event) {
		// 获取订单信息
		final OrderInfo orderInfo = event.getOrder();
		if (null == orderInfo) {
			log.warn("order not found!");
			return;
		}
		if (StrUtil.isEmpty(orderInfo.getCouponUserId())) {
			return;
		}
		// 查询用户优惠券
		CouponUser couponUser = couponUserService.getById(orderInfo.getCouponUserId());
		if (null == couponUser) {
			log.warn("user coupon not found! couponUserId: " + orderInfo.getCouponUserId());
			return;
		}
		if (!CouponUserStatusEnum.STATUS_3.getCode().equals(couponUser.getStatus())) {
			log.warn(" user coupon status error! couponUserId: " + orderInfo.getCouponUserId());
			return;
		}
		couponUser.setStatus(CouponUserStatusEnum.STATUS_1.getCode());
		couponUserService.updateById(couponUser);
	}

	@EventListener(HxOrderPayEvent.class)
	public void hxPayGoodsEventListener(HxOrderPayEvent event) {
		// 获取订单信息
		final OrderInfo orderInfo = event.getOrder();
		if (null == orderInfo) {
			log.warn("order not found!");
			return;
		}
		final List<OrderItem> orderItemList = event.getOrderItemList();
		// 增加销量
		orderItemList
			.forEach(orderItem -> goodsSpuService.updateSalesVolume(orderItem.getSpuId(), orderItem.getBuyQuantity()));
	}

	@EventListener(HxOrderPayEvent.class)
	public void hxPayDistributionEventListener(HxOrderPayEvent event) {
		// 获取订单信息
		final OrderInfo order = event.getOrder();

		final List<OrderItem> orderItemList = event.getOrderItemList();

		// 查询购买用户
		final UserInfo userInfo = userInfoService.getById(order.getUserId());
		if (ObjectUtil.isNull(userInfo)) {
			log.warn("user not found! userId: " + order.getUserId());
			return;
		}
		try {
			distributionOrderService.saveDistribution(order, orderItemList, userInfo, 1);
		}
		catch (Exception e) {
			log.error("error user distribution orderId: " + order.getId(), e);
		}
	}

}
